//
//  DGAccount.m
//  DGAccountKit
//
//  Created by Jinxiao on 11/19/13.
//  Copyright (c) 2013 debugeek. All rights reserved.
//

#import "DGAccount.h"
#import "DGAccountCredential.h"
#import "DGAccountManager.h"

NSString * const DGAccountKeyIdentifier = @"DGAccountKeyIdentifier";
NSString * const DGAccountKeyUserID = @"DGAccountKeyUserID";
NSString * const DGAccountKeyUsername = @"DGAccountKeyUsername";
NSString * const DGAccountKeyPassword = @"DGAccountKeyPassword";

NSString * const DGAccountKeyName = @"DGAccountKeyName";
NSString * const DGAccountKeyPickpic = @"DGAccountKeyPickpic";

@interface DGAccount ()
@property (nonatomic, readwrite, strong) NSString *identifier;
@end

@implementation DGAccount
+(DGAccount *)currentAccount{
    
    static DGAccount *user = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        user = [[DGAccount alloc]init];
    });
    return user;
}

- (id)init
{
    self = [super init];
    if(self)
    {
        CFUUIDRef uuidRef = CFUUIDCreate(nil);
        CFStringRef uuidString = CFUUIDCreateString(nil, uuidRef);
        self.identifier = (__bridge NSString *)uuidString;
        CFRelease(uuidString);
        CFRelease(uuidRef);
    }
    
    return self;
}

- (BOOL)validateAccount
{
    if([self.credential validateCredentials] && [self.credential.userID isEqualToString:self.userID] && self.credential && self.username)
    {
        return YES;
    }
    return NO;
}

- (BOOL)isCurrentAccount
{
    return (self == DGCurrentAccount) || [self.userID isEqualToString:DGCurrentAccount.userID];
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    [dictionary setSafeObject:self.identifier forKey:DGAccountKeyIdentifier];
    [dictionary setSafeObject:self.userID forKey:DGAccountKeyUserID];
    [dictionary setSafeObject:self.username forKey:DGAccountKeyUsername];
    [dictionary setSafeObject:self.password forKey:DGAccountKeyPassword];
    [dictionary setSafeObject:self.pickpic forKey:DGAccountKeyPickpic];
    [dictionary setSafeObject:self.name forKey:DGAccountKeyName];
    return dictionary;
}

- (void)updateWithDictionary:(NSDictionary *)dictionary
{
    self.identifier = [dictionary objectForKey:DGAccountKeyIdentifier];
    self.userID = [dictionary objectForKey:DGAccountKeyUserID];
    self.username = [dictionary objectForKey:DGAccountKeyUsername];
    self.password = [dictionary objectForKey:DGAccountKeyPassword];
    
    self.pickpic = [dictionary objectForKey:DGAccountKeyPickpic];
    self.name = [dictionary objectForKey:DGAccountKeyName];
    
    if(!self.credential)
    {
        self.credential = [[DGAccountCredential alloc] init];
        self.credential.userID = self.userID;
        [self.credential loadCredentials];
    }
}

- (void)setPhone:(NSString *)phone{
    [[NSUserDefaults standardUserDefaults] setObject:phone forKey:userPhone];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
- (void)clearPhone{
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:userPhone];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSString *)phone{
    return [[NSUserDefaults standardUserDefaults] objectForKey:userPhone];
}

- (void)setUser3rdURL:(NSString *)user3rdURL{

    [[NSUserDefaults standardUserDefaults] setObject:user3rdURL forKey:userURL];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)clear3rdURL{
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:userURL];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSString *)user3rdURL{
    return [[NSUserDefaults standardUserDefaults] objectForKey:userURL];
}
@end
